//
// Created by bjliuyong on 2021/6/21.
//

#include <cstring>
#include "../../include/jni/com_lemon_jvm_example_jni_HelloWorld.h"
#include "../../include/common.h"

/*
 * Class:     com_lemon_jvm_example_jni_HelloWorld
 * Method:    showVal
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_lemon_jvm_example_jni_HelloWorld_showVal
        (JNIEnv *env, jclass jclazz) {
    jfieldID val_field = env->GetStaticFieldID(jclazz, "val", "I");
    if (NULL == val_field) {
        ERROR_PRINT("出错了\n");

        exit(-1);
    }

    jint v = env->GetStaticIntField(jclazz, val_field);
    INFO_PRINT("===>val %d\n", v);
}

/*
 * Class:     com_lemon_jvm_example_jni_HelloWorld
 * Method:    changeVal
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_lemon_jvm_example_jni_HelloWorld_changeVal__
        (JNIEnv *env, jclass jclazz) {

    jfieldID val_field = env->GetStaticFieldID(jclazz, "val", "I");
    jint v = env->GetStaticIntField(jclazz, val_field);
    v += 100;
    env->SetStaticIntField(jclazz, val_field, v);
    return v;
}

/*
 * Class:     com_lemon_jvm_example_jni_HelloWorld
 * Method:    changeVal
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_com_lemon_jvm_example_jni_HelloWorld_changeVal__I
        (JNIEnv *env, jclass jclazz, jint v) {
    jfieldID val_field = env->GetStaticFieldID(jclazz, "val", "I");
    env->SetStaticIntField(jclazz, val_field, v);
    return v;
}

/*
 * Class:     com_lemon_jvm_example_jni_HelloWorld
 * Method:    showVal2
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_lemon_jvm_example_jni_HelloWorld_showVal2
        (JNIEnv *env, jobject _obj) {
    jclass jclazz = env->GetObjectClass(_obj);
    jfieldID val_field = env->GetFieldID(jclazz, "val2", "I");
    jint v = env->GetIntField(_obj,val_field);
    INFO_PRINT("v==> %d\n", v);
}

/*
 * Class:     com_lemon_jvm_example_jni_HelloWorld
 * Method:    changeVal2
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_com_lemon_jvm_example_jni_HelloWorld_changeVal2
        (JNIEnv *env, jobject _obj, jint _v) {
    jclass jclazz = env->GetObjectClass(_obj);
    jfieldID val_field = env->GetFieldID(jclazz, "val2", "I");
    env->SetIntField(_obj,val_field,_v);
    return _v;
}

/*
 * Class:     com_lemon_jvm_example_jni_HelloWorld
 * Method:    add
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_com_lemon_jvm_example_jni_HelloWorld_add
        (JNIEnv *env, jclass jclazz, jint v1, jint v2) {
    return v1 + v2;
}

/*
 * Class:     com_lemon_jvm_example_jni_HelloWorld
 * Method:    showName
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_lemon_jvm_example_jni_HelloWorld_showName__Ljava_lang_String_2
        (JNIEnv * env, jclass jclazz, jstring s){
    const char* ns= env->GetStringUTFChars(s, 0);
    INFO_PRINT("name ===> %s\n",ns);
}

/*
 * Class:     com_lemon_jvm_example_jni_HelloWorld
 * Method:    showName
 * Signature: ([B)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_lemon_jvm_example_jni_HelloWorld_showName___3B
        (JNIEnv * env, jclass jclazz, jbyteArray bytearray){

    char *chars = NULL;
    jbyte *bytes = env->GetByteArrayElements(bytearray, 0);
    int chars_len = env->GetArrayLength(bytearray);
    chars = new char[chars_len + 1];

    memset(chars,0,chars_len + 1);
    memcpy(chars, bytes, chars_len);
    chars[chars_len] = 0;
    INFO_PRINT("name ===> %s\n",chars);
    env->ReleaseByteArrayElements(bytearray, bytes, 0);

    return env->NewStringUTF(chars);
}





